/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import Models.*;
import Beans.*;
import java.util.Vector;
/**
 *
 * @author VIC
 */
public class DoiBan_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String act = request.getParameter("act");
            int langid = (Integer) session.getAttribute("langid");
             NhanVien nv = null;
            if (session.getAttribute("nv") != null) {
                 nv = (NhanVien) session.getAttribute("nv");
            }
           
            if(act.equals("gopBan")){
                String tuban = request.getParameter("tuban");
                String denban = request.getParameter("denban");
                String dsts = "'jointbsuc','jointbfal','selecttb','rightmsg'";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String orderfal = mdsts[0].getTenTS();
                String ordersuc = mdsts[1].getTenTS();
                String rightmsg = mdsts[2].getTenTS();
                String selecttb = mdsts[3].getTenTS();
                if(! NhanVien_Model.CheckQuyen(66, nv.getMaNV()))
                {
                    out.print(rightmsg);
                    return;
                }
                boolean kq1 = Ban_Model.KiemTraGopBan(tuban);
                boolean kq2 = Ban_Model.KiemTraGopBan(denban);
                if(!(kq1 && kq2))
                {
                    out.print(selecttb);
                    return;
                }
                
                int kq = Ban_Model.GopBan(tuban, denban);
                if(kq == 2)
                {
                    NhatKyHT_Model.GhiNhatKy(5, nv.getMaNV(), "table");
                    out.print(ordersuc);
                }
                else {
                    out.print(orderfal);
                }
            }
            else if(act.equals("chuyenBan")){
                String tuban = request.getParameter("tuban");
                String denban = request.getParameter("denban");
                String denkv = String.valueOf(Ban_Model.LayMaKV(denban)) ;
                String dsts = "'changetbsuc','changetbfal','rightmsg','selecttb'";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String orderfal = mdsts[0].getTenTS();
                String ordersuc = mdsts[1].getTenTS();
                String rightmsg = mdsts[2].getTenTS();
                String selecttb = mdsts[3].getTenTS();
                if(! NhanVien_Model.CheckQuyen(66, nv.getMaNV()))
                {
                    out.print(rightmsg);
                    return;
                }
                 boolean kq1 = Ban_Model.KiemTraGopBan(tuban);
                boolean kq2 = Ban_Model.KiemTraBan(denban);
                if(!(kq1 && kq2))
                {
                    out.print(selecttb);
                    return;
                }
                int kq = Ban_Model.ChuyenBan(tuban, denban,denkv);
                if(kq == 2)
                {
                    NhatKyHT_Model.GhiNhatKy(6, nv.getMaNV(), "table");
                    out.print(ordersuc);
                }
                else {
                    out.print(orderfal);
                }
            }
            else if(act.equals("tinhtien")){
                String ban = request.getParameter("ban");
                boolean kq = Ban_Model.UpdateTTBanMoi("8", ban);
                String dsts = "'waitbill','updatefal','rightmsg'";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String rightmsg = mdsts[0].getTenTS();
                String fal = mdsts[1].getTenTS();
                String wait = mdsts[2].getTenTS();
                if(kq)
                {
                    out.print(wait);
                }
                else {
                    out.print(fal);
                }
            }
            else if(act.equals("loadcart")){
                String ban = request.getParameter("ban");
                int item = Integer.parseInt(request.getParameter("item")) ;
                Vector v = PhieuDatMA_Model.LoadCart(ban,langid);
                request.setAttribute("cart", v);
                request.setAttribute("ban", ban);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("waiter/cartcnp.jsp");
                rd.forward(request, response);
            }
             else if(act.equals("hoithuc")){
                String ma = request.getParameter("ma");
                boolean kq = PhieuDatMA_Model.HoiThuc(ma);
                String dsts = "'rqsent','updatefal'";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String sent = mdsts[0].getTenTS();
                String fal = mdsts[1].getTenTS();
                if(kq)
                {
                    out.print(sent);
                }
                else {
                    out.print(fal);
                }
            }
             else if(act.equals("xoaMA")){
                String ma = request.getParameter("ma");
                boolean kq = PhieuDatMA_Model.XoaMA(ma);
                String dsts = "'cooked'";
                ThamSoHT[] mdsts = ThamSoHT_Model.DSThamSo(dsts, langid);
                String cook = mdsts[0].getTenTS();
                if(kq)
                {
                    out.print("1");
                }
                else {
                    out.print(cook);
                }
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
